package com.punai.customer.controller;

import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.customer.domain.CusMaterialLinkUnit;
import com.punai.customer.service.ICusMaterialLinkUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 材料单位换算Controller
 * 
 * @author zxw
 * @date 2022-12-04
 */
@RestController
@RequestMapping("/cus/materialLinkUnit")
public class CusMaterialLinkUnitController extends BaseController
{
    @Autowired
    private ICusMaterialLinkUnitService cusMaterialLinkUnitService;

    /**
     * 查询材料单位换算列表
     */
    @PreAuthorize("@ss.hasPermi('cus:materialLinkUnit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CusMaterialLinkUnit cusMaterialLinkUnit)
    {
        startPage();
        List<CusMaterialLinkUnit> list = cusMaterialLinkUnitService.selectCusMaterialLinkUnitList(cusMaterialLinkUnit);
        return getDataTable(list);
    }

    /**
     * 获取材料单位换算详细信息
     */
    @PreAuthorize("@ss.hasPermi('cus:materialLinkUnit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(cusMaterialLinkUnitService.selectCusMaterialLinkUnitById(id));
    }

    /**
     * 新增材料单位换算
     */
    @PreAuthorize("@ss.hasPermi('cus:materialLinkUnit:add')")
    @Log(title = "材料单位换算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CusMaterialLinkUnit cusMaterialLinkUnit)
    {
        return toAjax(cusMaterialLinkUnitService.insertCusMaterialLinkUnit(cusMaterialLinkUnit));
    }

    /**
     * 修改材料单位换算
     */
    @PreAuthorize("@ss.hasPermi('cus:materialLinkUnit:edit')")
    @Log(title = "材料单位换算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CusMaterialLinkUnit cusMaterialLinkUnit)
    {
        return toAjax(cusMaterialLinkUnitService.updateCusMaterialLinkUnit(cusMaterialLinkUnit));
    }

    /**
     * 删除材料单位换算
     */
    @PreAuthorize("@ss.hasPermi('cus:materialLinkUnit:remove')")
    @Log(title = "材料单位换算", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(cusMaterialLinkUnitService.deleteCusMaterialLinkUnitByIds(ids));
    }

    /**
     * 获取材料单位换算详细信息
     */
    @PreAuthorize("@ss.hasPermi('cus:materialLinkUnit:edit')")
    @GetMapping(value = "/getMaterialLinkUnitByItemId/{id}")
    public AjaxResult getMaterialLinkUnitByItemId(@PathVariable("id") String id)
    {
        return AjaxResult.success(cusMaterialLinkUnitService.getMaterialLinkUnitByItemId(id,getCompanyId()));
    }

    /**
     * 设置材料单位换算
     */
    @PreAuthorize("@ss.hasPermi('cus:materialLinkUnit:edit')")
    @Log(title = "材料单位换算", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/setTransition/{materialId}")
    public AjaxResult setTransition(@RequestBody List<CusMaterialLinkUnit> cusMaterialLinkUnitList,@PathVariable("materialId") String materialId)
    {
        Long companyId = getCompanyId();
        cusMaterialLinkUnitList.stream().forEach(cusMaterialLinkUnit -> cusMaterialLinkUnit.setCompanyId(companyId));
        return toAjax(cusMaterialLinkUnitService.setTransition(companyId,materialId,cusMaterialLinkUnitList));
    }
}
